package com.rcl.recyclerviewdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;

import com.rcl.recyclerviewdemo.adapter.RecyclerAdapter;
import com.rcl.recyclerviewdemo.bean.ContentBean;
import com.rcl.recyclerviewdemo.interfaces.IContentViewCallback;
import com.rcl.recyclerviewdemo.presenter.ContentPresenter;

import java.util.List;

public class MainActivity extends AppCompatActivity implements IContentViewCallback {

    private RecyclerAdapter mRecyclerAdapter;
    private ContentPresenter mInstance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        mRecyclerAdapter = new RecyclerAdapter();
        RecyclerView recycler = findViewById(R.id.recycler);
        //recycler.setLayoutManager(new GridLayoutManager(this,2));
        //recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        recycler.setAdapter(mRecyclerAdapter);

        mInstance = ContentPresenter.getInstance();
        mInstance.registeredActivity(this);
        mInstance.getContentData();

        //recycler添加和删除动画
        recycler.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void setContentData(List<ContentBean> contentBean) {
        mRecyclerAdapter.setData(contentBean);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mInstance.unregisteredActivity(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add:
                mRecyclerAdapter.addData(1);
                break;
            case R.id.remove:
                mRecyclerAdapter.removeData(1);
                break;
        }
        return true;
    }
}